Skip to content

GH1149 one-to-one related object not loaded with stateless session #1620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

seamus-hoban
Copy link
Contributor

@seamus-hoban seamus-hoban commented Mar 18, 2018

Fixes #1149

Here's my attempt to correct the above issue.
All unit tests run successfully, but I'm not sure that my change won't have other side-effects given that the fix is pretty much a crib from the stateful session, so any pointers would be most welcome.

Hopefully I've followed the contribution guidelines correctly.

@fredericDelaporte
Copy link
Member

Well, I was believing stateless sessions were not handling any kind of association, but the documentation only writes about collections. Granted, I have almost never used a stateless session.

Does many-to-one works? If no, having only one-to-one supported would be a bit awkward.

@seamus-hoban
Copy link
Contributor Author

I believe so - the project that I was working on where I came across the 1:1 bug was with a hierarchy that had a many to one relationship that was being loaded correctly.
I have added an object with a many-to-one relationship and a test for that to my code, should I update the pull-request with it?

@fredericDelaporte
Copy link
Member

No, no need. Ideally such a test should already exist somewhere in NHibernate tests, but I have not checked.

@seamus-hoban
Copy link
Contributor Author

I think the mappings and tests in NH1574 show retrieval of an object with a many-to-one association (SpecializedPrincipal). (StatelessTest.cs)

</id>
<property name="Name" />

<one-to-one name="Address" class="Address" property-ref="Company" cascade="all-delete-orphan"/>
Copy link
Member

@fredericDelaporte fredericDelaporte Mar 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May you add a test with an actual one-to-one in db? I mean, a one-to-one not using a property-ref but with one entity pk being also the pk of the other entity (so a pk which is also a fk), both side mapped as one-to-one with one side being constrained. (PersonTrueO2O with AddressTrueO2O in NH3931 tests is an example of this.)

Unless this case is already tested in NHibernate.Test and working (meaning the issue report was not really precise).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, PR updated.

@hazzik
Copy link
Member

hazzik commented Mar 18, 2018

From my point of view the changes are correct and consistent with other places where associations are involved.

@fredericDelaporte
Copy link
Member

Some notes of interest (which do not infirm this PR validity overall):

I have checked the many-to-one case: it does not work by default, because the association is loaded as a proxy and stateless session does not allow initializing a proxy. So it fails as soon as an access to a member of the association other than the id is done.

The one-to-one case is special in this respect, because it cannot be proxified from the non-constrained side. (It does not know if it exists without querying its table, so it loads it instead.) So from the non-constrained side, it is always directly loaded, instead of being proxified.

But the constrained side fails like a many-to-one by default.

Doing more checks, if the many-to-one is set as lazy="false", it does work (even without this PR), so we can consider that supporting accessing a non-constrained one-to-one on an entity loaded from a stateless session is consistent with the many-to-one case, because a non-constrained one-to-one is non lazy by default.

Copy link
Member

@fredericDelaporte fredericDelaporte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work, LGTM.

return null;
using (BeginProcess())
{
if (obj.IsProxy())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it another thought. I think the true branch is wrong/not possible/not needed.

@hazzik
Copy link
Member

hazzik commented Mar 19, 2018

Need async tests generation.

@hazzik
Copy link
Member

hazzik commented Mar 20, 2018

Hi @seamus-hoban. Please drop the last commit and regenerate the async tests. To regenerate async tests:

On windows:

  1. Run ShowBuildMenu.bat
  2. Press H on keyboard (option "H. Generate async code (Generates files in Async sub-folders)").
  3. Wait the tool to complete
  4. Commit and push generated code

On other platforms (mono):

  1. Navigate to ./Tools folder
  2. Run nuget install
  3. Navigate to ./src folder
  4. Run mono ../Tools/CSharpAsyncGenerator.CommandLine.0.8.2/tools/AsyncGenerator.CommandLine.exe
  5. Wait the tool to complete
  6. Commit and push generated code

@seamus-hoban
Copy link
Contributor Author

seamus-hoban commented Mar 20, 2018

Okay, will do sorry.

Just pushing the reverted test file now as I've errors on the ShowBuildMenu options above.

"unable to load one or more requested types' from Nant.NUnit2Tasks.dll.

And

"Msbuild failed when processing the file 'C:\Projects\nhibernate-core\src\NHibernate\NHibernate.csproj' with message: MSB0001: Internal MSBuild Error: Missing resource ''"

@fredericDelaporte
Copy link
Member

The first error is usual (at least on my machine) and does not prevent the task to execute. I do not know about the second one, I have never seen it. Is your tooling up to date?

I have pushed the async regeneration.

@fredericDelaporte
Copy link
Member

Ok, I guess you have updated Visual Studio to its yesterday release, 15.6.3. With 15.6.2, it was still working. Now that I have updated it myself, it is broken.

generate-async:

     [exec] 11:37:31,760 INFO  CommandLine:0 - AsyncGenerator
     [exec]
     [exec] 11:37:32,072 INFO  AsyncCodeGenerator:0 - Generating async code started
     [exec] 11:37:32,650 INFO  AsyncCodeGenerator:0 - Opening project 'E:\Projets\nhibernate\nhibernate-core\src\NHibernate\NHibernate.csproj' started
     [exec] 11:37:33,374 FATAL CommandLine:0 - System.InvalidOperationException: One or more errors occurred while opening the project:
     [exec] Msbuild failed when processing the file 'E:\Projets\nhibernate\nhibernate-core\src\NHibernate\NHibernate.csproj' with message: MSB0001: Internal MSBuild Error: Missing resource ''
     [exec] Hint: For suppressing irrelevant errors use SuppressDiagnosticFailures option.
     [exec]    à AsyncGenerator.AsyncCodeGenerator.CheckForErrors(MSBuildWorkspace workspace, String itemType, ImmutableArray`1 supressFailuresPredicates, ILogger logger)
     [exec]    à AsyncGenerator.AsyncCodeGenerator.<OpenProject>d__8.MoveNext()
     [exec] --- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
     [exec]    à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     [exec]    à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     [exec]    à AsyncGenerator.AsyncCodeGenerator.<GenerateAsync>d__0.MoveNext()
     [exec] --- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
     [exec]    à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     [exec]    à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     [exec]    à AsyncGenerator.CommandLine.Program.Main(String[] args)

@maca88, do you have the issue too?

@seamus-hoban
Copy link
Contributor Author

Thank you all!
Yeah 15.6.3 screws up any building from within Studio, but command line build is fine, but the AsyncGenerator stuff is as per your comment above. There's a thread on the developercommunity.visualstudio where are few others are affected.

@fredericDelaporte
Copy link
Member

fredericDelaporte commented Mar 20, 2018

Are you speaking of this thread? I think it is another (maybe related) trouble, since it has been opened before 15.6.3 release and is about project creation.

Otherwise could you link it?

I have not seen building in VS is broken too, I use primarily Rider. And well, I can still build with VS, even after having fully cleaned obj/bin folders in case it was reusing some assets compiled through Rider.

@fredericDelaporte
Copy link
Member

@hazzik, do you think this change is sufficiently mundane for going into 5.1.1? Or should it wait 5.2?

@seamus-hoban
Copy link
Contributor Author

@fredericDelaporte This thread: https://developercommunity.visualstudio.com/content/problem/218326/after-1563-publish-fails-due-to-error-projectasset.html
I can't build within studio even after deleting bin and obj folders.

@hazzik
Copy link
Member

hazzik commented Mar 20, 2018

This is kind-a a bug, so fine if it will be in 5.1.1.

@fredericDelaporte fredericDelaporte added this to the 5.1.1 milestone Mar 20, 2018
@fredericDelaporte fredericDelaporte merged commit 292e9b8 into nhibernate:master Mar 20, 2018
@maca88
Copy link
Contributor

maca88 commented Mar 20, 2018

@fredericDelaporte Yes, it happened also to me after the upgrade. The solution was to upgrade the Microsoft.Build package to the latest version as there is a bug with the older MSBuild. I released a new version 0.10.0, that includes the upgrade of Microsoft.Build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NH-3391 - StatelessSession: one-to-one detail-object is always null
4 participants